import db from "../../utils/db";

// initial state
const state = () => ({
  userInfo: {},
});

// getters
const getters = {
  getUserInfo(state) {
    return state.userInfo ? state.userInfo : db.get("userInfo");
  },
};

// mutations
const mutations = {
  setUserInfo(state, userInfo) {
    state.userInfo = userInfo;
  },
};

// actions
const actions = {
  setUserInfo({ commit }, userInfo) {
    commit("setUserInfo", userInfo);

    db.set("userInfo", userInfo);

    if (userInfo == null) {
      db.set("token", null);
    }
  },
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions,
};
